//package com.example.pets.model.datasource.DataSourceImpl;
//
//
//import androidx.lifecycle.LiveData;
//import androidx.lifecycle.MutableLiveData;
//
//import com.example.pets.model.datasource.PlaceDataSource;
//import com.example.pets.model.entities.Place;
//import com.google.firebase.firestore.CollectionReference;
//import com.google.firebase.firestore.FirebaseFirestore;
//import com.google.firebase.firestore.QueryDocumentSnapshot;
//
//import java.util.ArrayList;
//import java.util.List;
//
//public class PlaceDataSourceImpl implements PlaceDataSource {
//
//    private final CollectionReference placesCollection;
//
//    public PlaceDataSourceImpl() {
//        FirebaseFirestore db = FirebaseFirestore.getInstance();
//        placesCollection = db.collection("places");
//    }
//
//    @Override
//    public LiveData<Place> getPlaceById(String plid) {
//        MutableLiveData<Place> placeLiveData = new MutableLiveData<>();
//        placesCollection.document(plid)
//                .addSnapshotListener((snapshot, error) -> {
//                    if (error != null || snapshot == null || !snapshot.exists()) {
//                        placeLiveData.setValue(null);
//                        return;
//                    }
//                    Place place = snapshot.toObject(Place.class);
//                    placeLiveData.setValue(place);
//                });
//        return placeLiveData;
//    }
//
//    @Override
//    public LiveData<Boolean> addPlace(Place place) {
//        MutableLiveData<Boolean> resultLiveData = new MutableLiveData<>();
//        placesCollection.add(place)
//                .addOnSuccessListener(documentReference -> resultLiveData.setValue(true))
//                .addOnFailureListener(e -> resultLiveData.setValue(false));
//        return resultLiveData;
//    }
//
//    @Override
//    public LiveData<Boolean> updatePlace(String plid, Place place) {
//        MutableLiveData<Boolean> resultLiveData = new MutableLiveData<>();
//        placesCollection.document(plid)
//                .set(place)
//                .addOnSuccessListener(aVoid -> resultLiveData.setValue(true))
//                .addOnFailureListener(e -> resultLiveData.setValue(false));
//        return resultLiveData;
//    }
//
//    @Override
//    public LiveData<Boolean> deletePlace(String plid) {
//        MutableLiveData<Boolean> resultLiveData = new MutableLiveData<>();
//        placesCollection.document(plid)
//                .delete()
//                .addOnSuccessListener(aVoid -> resultLiveData.setValue(true))
//                .addOnFailureListener(e -> resultLiveData.setValue(false));
//        return resultLiveData;
//    }
//
//    @Override
//    public LiveData<List<Place>> getAllPlaces() {
//        MutableLiveData<List<Place>> placesLiveData = new MutableLiveData<>();
//        placesCollection.addSnapshotListener((querySnapshot, error) -> {
//            if (error != null || querySnapshot == null) {
//                placesLiveData.setValue(null);
//                return;
//            }
//            List<Place> places = new ArrayList<>();
//            for (QueryDocumentSnapshot doc : querySnapshot) {
//                Place place = doc.toObject(Place.class);
//                places.add(place);
//            }
//            placesLiveData.setValue(places);
//        });
//        return placesLiveData;
//    }
//}
